<template>
  <div class="project-selection-container">
    <!-- 页面标题 -->
    <h2 class="page-title">课题选择</h2>

    <!-- 筛选区域 -->
    <div class="filter-section">
      <div class="filter-item">
        <span class="filter-label">专业：</span>
        <select v-model="filters.major" class="filter-select">
          <option value="">全部</option>
          <option v-for="major in majors" :key="major" :value="major">{{ major }}</option>
        </select>
      </div>

      <div class="filter-item">
        <span class="filter-label">方向：</span>
        <select v-model="filters.direction" class="filter-select">
          <option value="">全部</option>
          <option v-for="direction in directions" :key="direction" :value="direction">{{ direction }}</option>
        </select>
      </div>

      <div class="filter-item">
        <span class="filter-label">教师：</span>
        <input v-model="filters.teacher" type="text" class="filter-input" placeholder="输入教师姓名">
      </div>

      <div class="filter-item">
        <span class="filter-label">关键词：</span>
        <input v-model="filters.keyword" type="text" class="filter-input" placeholder="输入关键词">
      </div>

      <button @click="applyFilters" class="filter-button">筛选</button>
    </div>

    <!-- 课题列表表格区域 -->
    <div class="project-table-container">
      <table class="project-table" v-if="pagedProjects.length > 0">
        <thead>
          <tr>
            <th>序号</th>
            <th>课题名称</th>
            <th>指导教师</th>
            <th>研究方向</th>
            <th>可选人数</th>
            <th>状态</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(project, index) in pagedProjects" :key="index">
            <td>{{ (currentPage - 1) * pageSize + index + 1 }}</td>
            <td>{{ project.name }}</td>
            <td>{{ project.teacher }}</td>
            <td>{{ project.direction }}</td>
            <td>{{ project.availableSlots }}</td>
            <td :class="['status', project.status === '可选' ? 'available' : 'unavailable']">
              {{ project.status }}
            </td>
            <td>
              <button @click="viewProjectDetails(project)" class="view-btn">查看详情</button>
            </td>
          </tr>
        </tbody>
      </table>
      <div v-else class="no-projects">
        <p>没有找到符合筛选条件的课题</p>
      </div>
      <el-pagination
        v-if="filteredProjects.length > pageSize"
        background
        layout="prev, pager, next"
        :total="filteredProjects.length"
        :page-size="pageSize"
        :current-page="currentPage"
        @current-change="handlePageChange"
        style="margin: 20px 0; text-align: right;"
      />
    </div>

    <!-- 课题详情区域 -->
    <div v-if="selectedProject" class="project-details">
      <h3 class="details-title">课题详情</h3>
      <div class="details-content">
        <div class="details-item">
          <span class="details-label">课题名称：</span>
          <span class="details-value">{{ selectedProject.name }}</span>
        </div>
        <div class="details-item">
          <span class="details-label">指导教师：</span>
          <span class="details-value">{{ selectedProject.teacher }}</span>
        </div>
        <div class="details-item">
          <span class="details-label">研究方向：</span>
          <span class="details-value">{{ selectedProject.direction }}</span>
        </div>
        <div class="details-item">
          <span class="details-label">课题简介：</span>
          <p class="details-value details-intro">{{ selectedProject.introduction }}</p>
        </div>
        <div class="details-item">
          <span class="details-label">研究内容：</span>
          <p class="details-value details-intro">{{ selectedProject.content }}</p>
        </div>
        <div class="details-item">
          <span class="details-label">预期目标：</span>
          <p class="details-value details-intro">{{ selectedProject.objective }}</p>
        </div>
        <div class="details-item">
          <span class="details-label">对学生能力要求：</span>
          <p class="details-value details-intro">{{ selectedProject.requirements }}</p>
        </div>
        <div class="details-item">
          <span class="details-label">所需软硬件条件：</span>
          <p class="details-value details-intro">{{ selectedProject.conditions }}</p>
        </div>
      </div>
      <div class="selection-actions">
        <button @click="addToSelection" class="select-btn">添加到意向课题</button>
        <button @click="applySelfDefined" class="self-define-btn">自拟题目申请</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';

const router = useRouter();
const selectedProject = ref(null); // 当前选中的课题
const filters = ref({
  major: '',
  direction: '',
  teacher: '',
  keyword: ''
}); // 筛选条件
const projects = ref([]); // 所有课题列表
const filteredProjects = ref([]); // 筛选后的课题列表

const pageSize = 10;
const currentPage = ref(1);
const pagedProjects = computed(() => {
  const start = (currentPage.value - 1) * pageSize;
  return filteredProjects.value.slice(start, start + pageSize);
});

// 模拟数据 - 在实际应用中，这部分数据应该从后端获取
const mockProjects = [
  {
    name: '智能校园导航系统设计与实现',
    teacher: '张教授',
    direction: '软件工程',
    status: '可选',
    availableSlots: 5,
    introduction: '本课题旨在设计并实现一个智能校园导航系统，帮助学生和访客在校园内快速找到目的地。系统将集成地图、定位和路径规划功能，提供友好的用户界面。',
    content: '研究校园导航系统的用户需求，设计系统架构，实现核心功能模块，包括地图展示、定位服务和路径规划算法等。',
    objective: '完成一个功能完整的校园导航原型系统，能够在实际校园环境中运行并提供导航服务。',
    requirements: '具备良好的编程能力，熟悉前端开发技术（如HTML、CSS、JavaScript）和后端开发技术（如Node.js、Python等），了解地图API的使用。',
    conditions: '需要配备智能手机或平板电脑用于测试，开发环境需安装相关IDE和工具链。'
  },
  {
    name: '基于深度学习的图像识别算法研究',
    teacher: '李教授',
    direction: '人工智能',
    status: '可选',
    availableSlots: 3,
    introduction: '本课题研究基于深度学习的图像识别算法，探索如何提高算法的准确性和效率，尤其是在复杂场景下的应用。',
    content: '学习和理解深度学习的基本原理和常用模型（如CNN、RNN等），研究图像预处理、特征提取和分类器设计等关键技术，实现并优化图像识别算法。',
    objective: '提出一种改进的图像识别算法，在特定数据集上的准确率较现有方法有显著提升，并完成实验验证。',
    requirements: '具备扎实的数学基础，熟悉机器学习和深度学习的基本概念，熟练使用Python编程语言和相关框架（如TensorFlow、PyTorch等）。',
    conditions: '需要配置高性能计算机（配备GPU）用于算法训练，安装相关深度学习框架和工具库。'
  }
  // 可以添加更多模拟课题
];

const majors = ref(['计算机科学与技术', '软件工程', '网络工程', '人工智能', '数据科学与大数据技术']); // 模拟专业列表
const directions = ref(['软件工程', '人工智能', '网络安全', '数据分析', '系统架构']); // 模拟方向列表

// 获取所有课题（模拟）
const fetchProjects = () => {
  projects.value = mockProjects;
  filteredProjects.value = [...mockProjects];
};

// 应用筛选条件
const applyFilters = () => {
  const { major, direction, teacher, keyword } = filters.value;
  filteredProjects.value = mockProjects.filter(project => {
    let match = true;
    if (major && project.direction !== major) match = false;
    if (direction && project.direction !== direction) match = false;
    if (teacher && !project.teacher.includes(teacher)) match = false;
    if (keyword && !project.name.includes(keyword)) match = false;
    return match;
  });
  currentPage.value = 1;
};

// 查看课题详情
const viewProjectDetails = (project) => {
  selectedProject.value = project;
};

// 添加到意向课题
const addToSelection = () => {
  if (selectedProject.value) {
    // 这里应调用后端接口
    ElMessage.success(`已将"${selectedProject.value.name}"添加到意向课题列表`);
  }
};

// 自拟题目申请
const applySelfDefined = () => {
  router.push('/project/self-define');
};

// 页面加载时获取课题列表
onMounted(() => {
  fetchProjects();
});

function handlePageChange(page) {
  currentPage.value = page;
}
</script>

<style scoped>
.project-selection-container {
  padding: 20px;
  max-width: 1000px;
  margin: 0 auto;
}

.page-title {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
}

.filter-section {
  background-color: #f5f7fa;
  padding: 15px;
  border-radius: 6px;
  margin-bottom: 20px;
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
  align-items: center;
}

.filter-item {
  display: flex;
  align-items: center;
}

.filter-label {
  margin-right: 8px;
  font-weight: bold;
  min-width: 60px;
}

.filter-select, .filter-input {
  padding: 8px 12px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
}

.filter-select {
  width: 150px;
}

.filter-input {
  width: 200px;
}

.filter-button {
  padding: 8px 16px;
  background-color: #4a90e2;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.filter-button:hover {
  background-color: #3a7bc8;
}

.project-table-container {
  margin-bottom: 30px;
}

.project-table {
  width: 100%;
  border-collapse: collapse;
  background-color: #fff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border-radius: 6px;
  overflow: hidden;
}

.project-table th, .project-table td {
  padding: 12px 15px;
  text-align: left;
  border-bottom: 1px solid #ebeef5;
}

.project-table th {
  background-color: #f5f7fa;
  font-weight: bold;
  color: #333;
}

.project-table tr:last-child td {
  border-bottom: none;
}

.project-table tr:hover {
  background-color: #f9fafa;
}

.status {
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 14px;
}

.status.available {
  background-color: #e1f3d8;
  color: #67c23a;
}

.status.unavailable {
  background-color: #fef0f0;
  color: #f56c6c;
}

.view-btn {
  padding: 6px 12px;
  background-color: #f0f2f5;
  color: #333;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.view-btn:hover {
  background-color: #e6e8eb;
}

.no-projects {
  text-align: center;
  padding: 30px;
  color: #999;
}

.project-details {
  background-color: #fff;
  border: 1px solid #ebeef5;
  border-radius: 6px;
  padding: 20px;
  margin-top: 20px;
}

.details-title {
  margin-top: 0;
  margin-bottom: 20px;
  font-size: 18px;
  color: #333;
}

.details-content {
  margin-bottom: 20px;
}

.details-item {
  margin-bottom: 15px;
}

.details-label {
  font-weight: bold;
  margin-right: 10px;
  display: inline-block;
  width: 120px;
}

.details-value {
  color: #666;
}

.details-intro {
    line-height: 1.6;
    margin-top: 5px;
  }

.selection-actions {
  display: flex;
  gap: 15px;
}

.select-btn, .self-define-btn {
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.select-btn {
  background-color: #4a90e2;
  color: white;
}

.select-btn:hover {
  background-color: #3a7bc8;
}

.self-define-btn {
  background-color: #f0f2f5;
  color: #333;
}

.self-define-btn:hover {
  background-color: #e6e8eb;
}
</style>